% measured trinitron - edf 03.03.09
% sony trinitron on right card output, no extender cable, contrast 100, brightness 50
% multisync FE2111SB on left card output
% both have small white dvi/vga adapters
% both monitors on, warmed up over weekend
% other light sources in room: LED, phys comp, eyetracking comp.  didn't draw curtain
% nvidia gforce 8600GTS (unsure of mfg - xfx? black case/gigabyte mobo) at dual 100Hz 1280x1024
%
% ratrix trunk rev 1992
%
% spyderData=useSpyder('CRT', 1, repmat(linspace(0,1,2^8)',1,3))
% spyderCdPerMsquared=spyderData(end-255:end,2);

function newLUT=makeLinearizedLUT(monitorID)
vals=0:255;

switch monitorID
    case 'trinitron'
        spyderCdPerMsquared=[
            2.563
            2.673
            2.781
            2.913
            3.027
            3.147
            3.287
            3.422
            3.551
            3.701
            3.845
            3.989
            4.137
            4.282
            4.441
            4.602
            4.775
            4.945
            5.125
            5.292
            5.473
            5.654
            5.928
            6.1
            6.23
            6.433
            6.635
            6.854
            7.058
            7.271
            7.496
            7.713
            7.947
            8.171
            8.425
            8.652
            8.898
            9.149
            9.401
            9.664
            9.918
            10.191
            10.452
            10.733
            10.995
            11.283
            11.569
            11.857
            12.154
            12.472
            12.776
            13.075
            13.388
            13.718
            14.032
            14.362
            14.681
            15.023
            15.374
            15.72
            16.055
            16.419
            16.783
            17.142
            17.609
            17.991
            18.372
            18.758
            19.144
            19.545
            19.938
            20.356
            20.756
            21.164
            21.593
            22.009
            22.45
            22.879
            23.317
            23.767
            24.221
            24.683
            25.129
            25.602
            26.069
            26.551
            27.036
            27.511
            27.985
            28.486
            28.986
            29.491
            29.992
            30.516
            31.04
            31.564
            32.105
            32.636
            33.19
            33.745
            34.288
            34.843
            35.421
            36
            36.557
            37.121
            37.716
            38.323
            38.917
            39.532
            40.127
            40.75
            41.362
            41.995
            42.632
            43.271
            43.916
            44.568
            45.212
            45.884
            46.507
            47.171
            47.857
            48.543
            49.216
            49.923
            50.615
            51.316
            52.211
            52.922
            53.658
            54.384
            55.123
            55.882
            56.634
            57.381
            58.133
            58.896
            59.681
            60.461
            61.237
            62.037
            62.821
            63.627
            64.405
            65.209
            66.057
            66.869
            67.694
            68.524
            69.365
            70.21
            71.066
            71.934
            72.816
            73.693
            74.56
            75.439
            76.324
            77.232
            78.095
            78.999
            79.936
            80.852
            81.752
            82.701
            83.654
            84.601
            85.547
            86.516
            87.483
            88.444
            89.436
            90.424
            91.43
            92.414
            93.392
            94.398
            95.418
            96.45
            97.476
            98.516
            99.553
            100.611
            101.694
            102.764
            103.839
            104.923
            105.999
            107.091
            108.199
            109.29
            110.611
            111.737
            112.884
            113.985
            115.105
            116.236
            117.405
            118.567
            119.745
            120.931
            122.111
            123.276
            124.448
            125.664
            126.845
            128.053
            129.27
            130.494
            131.73
            132.964
            134.183
            135.428
            136.692
            137.934
            139.231
            140.513
            141.817
            143.072
            144.363
            145.644
            146.944
            148.278
            149.564
            150.876
            152.236
            153.544
            154.909
            156.233
            157.574
            158.935
            160.324
            161.653
            163.021
            164.422
            165.789
            167.186
            168.616
            170.003
            171.378
            172.806
            174.254
            175.667
            177.148
            178.603
            180.049
            181.492
            182.957
            184.441
            185.899
            187.376
            188.822
            190.325
            191.827
            193.321];
    otherwise
        error('unknown monitor ID')
end

if all(diff(spyderCdPerMsquared)>0) && length(spyderCdPerMsquared)==length(vals)
    desiredVals=linspace(spyderCdPerMsquared(1),spyderCdPerMsquared(end),length(spyderCdPerMsquared));
    newLUT = interp1(spyderCdPerMsquared,vals,desiredVals,'linear')/vals(end); %consider pchip
    newLUT = repmat(newLUT',1,3);
else
    error('vals not monotonic -- should fit parametrically or check that data collection OK')
end

% above linearization gives following quite linear fit: (interp1('linear') not 'pchip')
% bwData=[
%     2.577
%     3.309
%     4.076
%     4.844
%     5.593
%     6.31
%     7.097
%     7.807
%     8.574
%     9.311
%     10.097
%     10.839
%     11.623
%     12.367
%     13.138
%     13.841
%     14.677
%     15.342
%     16.121
%     16.835
%     17.568
%     18.248
%     19.021
%     19.813
%     20.517
%     21.34
%     22.086
%     22.842
%     23.507
%     24.284
%     25.105
%     25.795
%     26.506
%     27.358
%     28.073
%     28.818
%     29.588
%     30.354
%     31.142
%     31.792
%     32.616
%     33.288
%     34.127
%     34.823
%     35.532
%     36.394
%     37.088
%     37.82
%     38.578
%     39.329
%     40.076
%     40.846
%     41.648
%     42.444
%     43.065
%     43.856
%     44.674
%     45.342
%     46.179
%     46.962
%     47.636
%     48.316
%     49.17
%     49.86
%     50.56
%     51.437
%     51.966
%     52.7
%     53.413
%     54.135
%     55.065
%     55.619
%     56.373
%     57.12
%     57.897
%     58.657
%     59.433
%     60.198
%     60.989
%     61.777
%     62.567
%     63.185
%     63.988
%     64.763
%     65.571
%     66.186
%     67.026
%     67.856
%     68.471
%     69.313
%     69.939
%     70.813
%     71.454
%     72.328
%     72.958
%     73.846
%     74.489
%     75.376
%     76.053
%     76.733
%     77.641
%     78.263
%     78.952
%     79.879
%     80.562
%     81.275
%     82.172
%     82.882
%     83.579
%     84.279
%     85
%     85.753
%     86.459
%     87.421
%     88.159
%     88.906
%     89.623
%     90.342
%     91.092
%     91.863
%     92.607
%     93.334
%     94.093
%     94.867
%     95.626
%     96.363
%     97.143
%     97.917
%     98.709
%     99.235
%     100.048
%     100.812
%     101.628
%     102.431
%     103.229
%     103.744
%     104.566
%     105.382
%     106.2
%     106.76
%     107.57
%     108.411
%     109.205
%     109.797
%     110.578
%     111.137
%     111.963
%     112.533
%     113.375
%     114.255
%     114.797
%     115.656
%     116.535
%     117.114
%     117.976
%     118.564
%     119.43
%     120.305
%     120.895
%     121.742
%     122.36
%     123.234
%     123.805
%     124.694
%     125.294
%     126.226
%     126.825
%     127.706
%     128.32
%     129.261
%     129.85
%     130.779
%     131.39
%     132.292
%     132.95
%     133.538
%     134.462
%     135.071
%     136.019
%     136.658
%     137.589
%     138.228
%     138.869
%     139.811
%     140.473
%     141.101
%     142.069
%     142.688
%     143.353
%     144.29
%     144.932
%     145.594
%     146.568
%     147.225
%     147.868
%     148.554
%     149.514
%     150.166
%     150.822
%     151.799
%     152.469
%     153.138
%     153.798
%     154.815
%     155.465
%     156.125
%     156.792
%     157.833
%     158.495
%     159.178
%     159.855
%     160.561
%     161.568
%     162.243
%     162.932
%     163.601
%     164.314
%     165.333
%     166.004
%     166.683
%     167.425
%     168.093
%     168.833
%     169.521
%     170.592
%     171.277
%     171.971
%     172.702
%     173.396
%     174.123
%     174.813
%     175.556
%     176.602
%     177.302
%     178.034
%     178.753
%     179.46
%     180.218
%     180.907
%     181.65
%     182.396
%     183.147
%     183.831
%     184.587
%     185.323
%     186.06
%     186.8
%     187.548
%     188.249
%     189.003
%     189.736
%     190.489
%     191.218
%     192.006
%     192.779
%     193.536
%     ];
% 
% x=1:length(bwData);
% [p,S,mu] = polyfit(x',bwData,1);
% subplot(2,1,1)
% plot(x',polyval(p,x',S,mu),'b')
% hold on
% plot(x',bwData,'r')
% subplot(2,1,2)
% plot(bwData'-polyval(p,x,S,mu))